package com.gem.talkbase.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.gem.talkbase.R;
import com.gem.talkbase.bean.Firend;
import com.gem.talkbase.client.Client;
import com.gem.talkbase.service.JSON_Friend_info;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;

import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 *
 */
public class EditRingActivity extends  Activity  implements OnClickListener{
	ImageView iView_poto;
	String urlphone,photoname,photoname1,time1,time;
	TextView sendView;
	EditText editText;
	SharedPreferences sharedPreferences; 
	 String username,userid,userimage;
	//把图片上传至服务器的路径
	private static final String Url =Client.FirendRing_URL;
	private String requestPath=Client.FirendRingInfo_URL;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		setContentView(R.layout.write_time_photo);
		init();  
		sharedPreferences = getSharedPreferences(Client.SAVE_USER,MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");
		userid=sharedPreferences.getString("userid", "");  //这 是登录者的手机号
		userimage=sharedPreferences.getString("userimage", "");  //这 是登录者的手机号
		System.out.println(userimage+"++++++++++++=======================================================================userimage");
//		Intent intent=getIntent();
//	    urlphone=intent.getStringExtra("imagename");
//	    photo=intent.getStringExtra("imageurl");
		Bitmap photo=(Bitmap)getIntent().getParcelableExtra(FirendRing_Activity.PAR_KEY);
        urlphone=getIntent().getStringExtra("imagename");
        photoname=getIntent().getStringExtra("photoname");
//        photoname=replaceBlank(photoname1);
        time=photoname;
//       time=replaceBlank(time1);
//        time=getIntent().getStringExtra("photoname");
//        photoname=replaceBlank(time);
        System.out.println(photoname+"===================================phonename");
	    System.out.println("photo---111--"+ photo );
		Drawable drawable = new BitmapDrawable(photo);
		iView_poto.setImageDrawable(drawable);
		
	}
	

	
	public static String replaceBlank(String str) {  
		String dest = "";  
		if (str!=null) {  
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
			Matcher m = p.matcher(str);  
			dest = m.replaceAll("");  
		}  
		return dest;  
	}  
	
	public void init(){
		iView_poto=(ImageView)findViewById(R.id.getPicture);
	    sendView=(TextView)findViewById(R.id.save);
	    sendView.setOnClickListener(this);
	    
	    editText=(EditText)findViewById(R.id.myNoteTime);
	}


	@Override
	public void onClick(View v) {
		switch ( v.getId())  {
		case R.id.save : 
			upload(urlphone); 
			break ;
		}
		
	}
	 
	/*把手机SD卡中的照片上传到服务器
	 * */
	void upload( String urlPhoto) {
		System.out.println(photoname+"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
		AjaxParams params = new AjaxParams();
//		params.put("userName","junge");
//		params.put("action","action");
//		params.put("photoname",photoname);
		System.out.println("上传手机SD卡中的图片的路径是 ： "+urlPhoto);
		
		if(urlPhoto != null) {
			try {
				params.put("picture",new File(urlPhoto));
			} catch (FileNotFoundException e) {
				e.printStackTrace();  
				System.out.println("文件路径找不到");
			}      
			FinalHttp fh = new FinalHttp();
			fh.post( Url , params, new AjaxCallBack<Object>() {
				public void onSuccess(Object t) {
					Toast.makeText(EditRingActivity.this, "上传成功！",Toast.LENGTH_SHORT).show();
					fun();
				};
				public void onFailure(Throwable t, String strMsg) {
					Toast.makeText(EditRingActivity.this, "上传失败！",Toast.LENGTH_SHORT).show();
				};
			} ) ;
		}
		else Toast.makeText(EditRingActivity.this, "没有可上传的图片！",Toast.LENGTH_SHORT).show();
	}
	
	
	public void fun(){
		String  editString = EditRingActivity.this.editText.getText().toString();
		AjaxParams params = new AjaxParams();
		params.put("edit", editString);//文字
		params.put("time", time);
		params.put("username", username);
		params.put("userid", userid);
		params.put("userimage", userimage);
		params.put("photoname", photoname);
		FinalHttp fh = new FinalHttp();
		fh.post(requestPath, params, new AjaxCallBack<String>() {
			@Override
			public void onFailure(Throwable t, String strMsg) {
				System.out.println("加载失败！！！");
				super.onFailure(t, strMsg);
			}
			@Override  
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				System.out.println("正在加载！！！");
				super.onLoading(count, current);
			}
			
			@Override
			public void onSuccess(String jsonString) {
	 			super.onSuccess(jsonString);
              Intent intent =new Intent(EditRingActivity.this,FirendRing_Activity.class);
              startActivity(intent);
              finish();
			}
		});
	}
	
	
}
